﻿using System;
using System.Drawing;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;

using SmartParking.Data.Entities;
using SmartParking.Bussiness;

namespace SmartParking.Web.Manager
{
    public partial class Security : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                (Master as Logger).Title = "Security";
                (Master as Logger).ActiveMenuItem = "Security";
                LoadRoles();
                LoadPermissions();
            }
        }

        protected void LoadRoles()
        {
            SecurityManager.BindRoles(ref grdRoles);
        }

        protected void LoadPermissions()
        {
            SecurityManager.BindPermissions(ref grdPermission);
        }

        protected void grdRoles_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "EditRole")
            {
                Role role = SecurityManager.GetRole(Convert.ToInt32(e.CommandArgument));
                if (role != null)
                {                
                    txtRoleName.Text = role.Name;
                    hfRoleId.Value = role.RoleID.ToString();
                }
            }
            else if (e.CommandName == "DeleteRole")
            {
                SecurityManager.DeleteRole(Convert.ToInt32(e.CommandArgument), Profile.ManagerID, Request.UserHostAddress);
                LoadRoles();
            }
        }

        protected void grdPermission_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "EditPermission")
            {
                Permission permission = SecurityManager.GetPermission(Convert.ToInt32(e.CommandArgument));
                if (permission != null)
                {
                    txtPermissionName.Text = permission.Name;
                    hfPermissionId.Value = permission.PermissionID.ToString();
                }
            }
            else if (e.CommandName == "DeletePermission")
            {
                SecurityManager.DeletePermission(Convert.ToInt32(e.CommandArgument), Profile.ManagerID, Request.UserHostAddress);
                LoadPermissions();
            }
        }

        protected void lnkAddRole_Click(object sender, EventArgs e)
        {
            SecurityManager.SaveRole(hfRoleId.Value, txtRoleName.Text, Profile.ManagerID, Request.UserHostAddress);

            LoadRoles();

            hfRoleId.Value = txtRoleName.Text = string.Empty;
        }

        protected void lbtnAddPermission_Click(object sender, EventArgs e)
        {
            SecurityManager.SavePermission(hfPermissionId.Value, txtPermissionName.Text, Profile.ManagerID, Request.UserHostAddress);

            LoadPermissions();

            hfPermissionId.Value = txtPermissionName.Text = string.Empty;
        }
    }
}
